'''
Author: your name
Date: 2024-10-13 16:05:56
LastEditTime: 2024-10-13 16:15:40
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \python_script\transfer.py
'''
# -*- coding: utf-8 -*-
import cv2
import numpy as np

def save_raw_as_bmp(raw_data, width, height, bmp_filename):
    # 假设图像是24位的BGR格式
    bytes_per_pixel = 3
    # 创建一个NumPy数组，形状为高度x宽度x3，数据类型为uint8
    image_array = np.frombuffer(raw_data, dtype=np.uint8).reshape((height, width, bytes_per_pixel))
    # 将NumPy数组转换为OpenCV的Mat对象
    # image = cv2.cvtColor(image_array, cv2.COLOR_RGB2BGR)  # 如果数据已经是BGR格式，则不需要转换
    # 使用OpenCV保存图像
    cv2.imwrite(bmp_filename, image_array)

if __name__ == '__main__':
    raw_filename = 'image37.raw'
    bmp_filename = 'image.bmp'
    image_width = 1920  # 图像宽度
    image_height = 1200  # 图像高度

    # 读取原始图像数据
    with open(raw_filename, 'rb') as f:
        raw_data = f.read()

    save_raw_as_bmp(raw_data, image_width, image_height, bmp_filename)
    print("Converted {} to {}".format(raw_filename, bmp_filename))